package com.example.liucxu.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * Created by liucxu on 2018/2/2.
 */

public class CustomTextView extends View {


    private int text_bg_color;
    private int text_text_color;
    private String text;
    private int size_text;

    private Paint mPaint;
    private Rect rect;

    public CustomTextView(Context context) {
        this(context,null);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.CustomTextView);
        if(typedArray!=null){
            text_bg_color=typedArray.getColor(R.styleable.CustomTextView_custom_textview_text_bg_color, Color.GRAY);
            text_text_color=typedArray.getColor(R.styleable.CustomTextView_custom_textview_text_color,Color.WHITE);
            text=typedArray.getString(R.styleable.CustomTextView_custom_textview_text);
            size_text=typedArray.getDimensionPixelSize(R.styleable.CustomTextView_custom_textview_text_size,45);
            typedArray.recycle();
        }
        rect=new Rect();
        mPaint=new Paint();
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                text=randomText();
                invalidate();
            }
        });
    }

    private String randomText()
    {
        Random random = new Random();
        Set<Integer> set = new HashSet<Integer>();
        while (set.size() < 4)
        {
            int randomInt = random.nextInt(10);
            set.add(randomInt);
        }
        StringBuffer sb = new StringBuffer();
        for (Integer i : set)
        {
            sb.append("" + i);
        }

        return sb.toString();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(text_bg_color);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas.drawRect(0,0,getMeasuredWidth(), getMeasuredHeight(),mPaint);
        mPaint.setColor(text_text_color);
        mPaint.setTextSize(size_text);
        mPaint.getTextBounds(text,0,text.length(),rect);
        canvas.drawText(text,getWidth()/2-rect.width()/2,getHeight()/2+rect.height()/2,mPaint);
    }

//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        int widthMode=MeasureSpec.getMode(widthMeasureSpec);
//        int widthSize=MeasureSpec.getSize(widthMeasureSpec);
//        int heightMode=MeasureSpec.getMode(heightMeasureSpec);
//        int heightSize=MeasureSpec.getSize(heightMeasureSpec);
//        int width=widthSize;
//        int height=heightSize;
//        if(widthMode==MeasureSpec.EXACTLY){
//            width=widthSize;
//        }
//        else {
//            mPaint.setTextSize(size_text);
//            mPaint.getTextBounds(text,0,text.length(),rect);
//            width=getPaddingLeft()+rect.width()+getPaddingRight();
//        }
//        if(heightMode==MeasureSpec.EXACTLY){
//            height=heightSize;
//        }
//        else{
//            mPaint.setTextSize(size_text);
//            mPaint.getTextBounds(text,0,text.length(),rect);
//            height=getPaddingTop()+rect.height()+getPaddingBottom();
//        }
//        setMeasuredDimension(width, height);
//    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }
}
